package com.boyuan.huijiaoyu;

import org.apache.http.impl.conn.DefaultClientConnection;

import com.example.contactfragment.ContactService;
import com.magicsoft.app.wcf.listener.GetMultilParamsListener;
import com.magicsoft.constant.Constant;

import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.IntentSender.SendIntentException;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class YaoyiyaoActivity extends Activity implements OnClickListener{

	private ImageView imag_hudong;
	private ImageView imag_xinwen;
	private ImageView imag_shiping;
	private ImageView imag_zhuanjia;
	private SensorManager sensorManager;
	private Vibrator vibrator;
	private ContactService contactService;
	static int a = 1;
	String url_huodong = Constant.BaseUrl + "resource/activity/";
	String url_xinwen = Constant.BaseUrl + "resource/article/";
	String url_shiping = Constant.BaseUrl + "resource/video/";
	String url_zhuanjia = Constant.BaseUrl + "resource/expert/";
	boolean isfirst1 = true;
	boolean isfirst2 = true;
	boolean isfirst3 = true;
	boolean isfirst4 = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_yaoyiyao);
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
		initUI();
	}

	private void initUI() {
		findViewById(R.id.lay_back).setOnClickListener(this);
		findViewById(R.id.lay_huodong).setOnClickListener(this);
		findViewById(R.id.lay_xinwen).setOnClickListener(this);
		findViewById(R.id.lay_shiping).setOnClickListener(this);
		findViewById(R.id.lay_zhuanjia).setOnClickListener(this);
		tv_hudong = (TextView) findViewById(R.id.tv_huodong);
		tv_xinwen = (TextView) findViewById(R.id.tv_xinwen);
		tv_shiping = (TextView) findViewById(R.id.tv_shiping);
		tv_zhuanjia = (TextView) findViewById(R.id.tv_zhuanjia);
		imag_hudong = (ImageView) findViewById(R.id.imageView3);
		imag_xinwen = (ImageView) findViewById(R.id.imageView4);
		imag_shiping = (ImageView) findViewById(R.id.imageView5);
		imag_zhuanjia = (ImageView) findViewById(R.id.imageView6);
		imag_yaoyiyao = (ImageView) findViewById(R.id.imageView7);
		video = (RelativeLayout) findViewById(R.id.video);
		webview = (WebView) findViewById(R.id.webView1);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if(sensorManager != null){//注册监听器
			sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
			//第一个参数是Listener，第二个参数是所得传感器类型，第三个参数值获取传感器信息的频率
		}
		webview.onResume();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if(sensorManager != null){
			sensorManager.unregisterListener(sensorEventListener);
		}
		webview.onPause();
	}

	
	/**
	 * 重力感应监听
	 */
	private SensorEventListener sensorEventListener = new SensorEventListener() {

		@Override
		public void onSensorChanged(SensorEvent event) {
			// 传感器信息改变时执行该方法
			float[] values = event.values;
			float x = values[0]; // x轴方向的重力加速度，向右为正
			float y = values[1]; // y轴方向的重力加速度，向前为正
			float z = values[2]; // z轴方向的重力加速度，向上为正
			// 一般在这三个方向的重力加速度达到40就达到了摇晃手机的状态。
			int medumValue = 19;// 三星 i9250怎么晃都不会超过20，没办法，只设置19了
			if (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z) > medumValue) {
				vibrator.vibrate(200);
				Message message = new Message();
				message.what = a;
				handler.sendMessage(message);
			}else{
				vibrator.cancel();
			}
		}
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			
		};
	};
	
	/**
	 * 动作执行
	 */
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				initWebview(url_huodong);
				isfirst1 = false;
				isfirst2 = true;
				isfirst3 = true;
				isfirst4 = true;
				break;
			case 2:
				initWebview(url_xinwen);
				isfirst1 = true;
				isfirst2 = false;
				isfirst3 = true;
				isfirst4 = true;
				break;
			case 3:
				initWebview(url_shiping);
				isfirst1 = true;
				isfirst2 = true;
				isfirst3 = false;
				isfirst4 = true;
				break;
			case 4:
				initWebview(url_zhuanjia);
				isfirst1 = true;
				isfirst2 = true;
				isfirst3 = true;
				isfirst4 = false;
				break;
			default:
				break;
			}
		}

	};
	
	public void initWebview(String url){
		imag_yaoyiyao.setVisibility(View.GONE);
		webview.setVisibility(View.VISIBLE);
		getHuodong(url);
		webview.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				return super.shouldOverrideUrlLoading(view, url);
			}
			
		});
	}
	
	private ImageView imag_yaoyiyao;
	private WebView webview;
	private RelativeLayout video;
	private TextView tv_hudong;
	private TextView tv_xinwen;
	private TextView tv_shiping;
	private TextView tv_zhuanjia;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.yaoyiyao, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lay_back:
		
			finish();
			break;
		case R.id.lay_huodong:
			huodong();
			break;
		case R.id.lay_xinwen:
			xinwen();
			break;
		case R.id.lay_shiping:
			shiping();
			break;
		case R.id.lay_zhuanjia:
			zhuanjia();
			break;
		default:
			break;
		}
	}

	private void zhuanjia() {
		tv_hudong.setTextColor(Color.parseColor("#bfbfbf"));
		tv_xinwen.setTextColor(Color.parseColor("#bfbfbf"));
		tv_shiping.setTextColor(Color.parseColor("#bfbfbf"));
		tv_zhuanjia.setTextColor(Color.parseColor("#52B535"));
		imag_hudong.setImageResource(R.drawable.huodong_unselect);
		imag_xinwen.setImageResource(R.drawable.xinwen_unselect);
		imag_shiping.setImageResource(R.drawable.shipin_unselect);
		imag_zhuanjia.setImageResource(R.drawable.zhuanjia_select);
		a = 4;
		if(isfirst4 == true){
//			webview.reload();
			imag_yaoyiyao.setVisibility(View.VISIBLE);
			webview.setVisibility(View.GONE);
		}else{
			imag_yaoyiyao.setVisibility(View.GONE);
			webview.setVisibility(View.VISIBLE);
		}
	}

	private void shiping() {
		tv_hudong.setTextColor(Color.parseColor("#bfbfbf"));
		tv_xinwen.setTextColor(Color.parseColor("#bfbfbf"));
		tv_shiping.setTextColor(Color.parseColor("#52B535"));
		tv_zhuanjia.setTextColor(Color.parseColor("#bfbfbf"));
		imag_hudong.setImageResource(R.drawable.huodong_unselect);
		imag_xinwen.setImageResource(R.drawable.xinwen_unselect);
		imag_shiping.setImageResource(R.drawable.shipin_select);
		imag_zhuanjia.setImageResource(R.drawable.zhuanjia_unselect);
		a = 3;
		if(isfirst3 == true){
			imag_yaoyiyao.setVisibility(View.VISIBLE);
			webview.setVisibility(View.GONE);
		}else{
			imag_yaoyiyao.setVisibility(View.GONE);
			webview.setVisibility(View.VISIBLE);
		}
	}

	private void xinwen() {
		tv_hudong.setTextColor(Color.parseColor("#bfbfbf"));
		tv_xinwen.setTextColor(Color.parseColor("#52B535"));
		tv_shiping.setTextColor(Color.parseColor("#bfbfbf"));
		tv_zhuanjia.setTextColor(Color.parseColor("#bfbfbf"));
		imag_hudong.setImageResource(R.drawable.huodong_unselect);
		imag_xinwen.setImageResource(R.drawable.xinwen_select);
		imag_shiping.setImageResource(R.drawable.shipin_unselect);
		imag_zhuanjia.setImageResource(R.drawable.zhuanjia_unselect);
		a = 2;
		if(isfirst2 == true){
//			webview.reload();
			imag_yaoyiyao.setVisibility(View.VISIBLE);
			webview.setVisibility(View.GONE);
		}else{
			imag_yaoyiyao.setVisibility(View.GONE);
			webview.setVisibility(View.VISIBLE);
		}
	}

	private void huodong() {
		tv_hudong.setTextColor(Color.parseColor("#52B535"));
		tv_xinwen.setTextColor(Color.parseColor("#bfbfbf"));
		tv_shiping.setTextColor(Color.parseColor("#bfbfbf"));
		tv_zhuanjia.setTextColor(Color.parseColor("#bfbfbf"));
		imag_hudong.setImageResource(R.drawable.huodong_select);
		imag_xinwen.setImageResource(R.drawable.xinwen_unselect);
		imag_shiping.setImageResource(R.drawable.shipin_unselect);
		imag_zhuanjia.setImageResource(R.drawable.zhuanjia_unselect);
		a = 1;
		if(isfirst1 == true){
//			webview.reload();
			imag_yaoyiyao.setVisibility(View.VISIBLE);
			webview.setVisibility(View.GONE);
		}else{
			imag_yaoyiyao.setVisibility(View.GONE);
			webview.setVisibility(View.VISIBLE);
		}
	}
	
	
	public void getHuodong(String url){
		if (contactService == null) {
			contactService = new ContactService(getApplicationContext());
		}
		
		contactService.getYaoyiyao(url, "0", new GetMultilParamsListener() {
			
			@SuppressLint({ "SetJavaScriptEnabled", "InlinedApi", "NewApi" })
			@Override
			public void onFinish(Object... params) {
				if(params != null){
					Huodong huodong = (Huodong) params[0];
					StringBuffer sb = new StringBuffer();
					String content = huodong.getContent();
					String copyURL = huodong.getCopyURL();
					Log.e("copyURL", copyURL);
					content.replaceAll("&lt;", "<");
					content.replaceAll("&gt;", ">");
					String data1 = content.replaceAll("&lt;", "<");
					String data2 = data1.replaceAll("&gt;", ">");
					String data3 = data2.replaceAll("&quot;", "\"");
					String data4 = data3.replaceAll("&amp;", "&");
					String data5 = data4.replaceAll("&nbsp;", " ");
					sb.append(data5);
					String data = sb.toString();
					Log.e("data", data);
					if("".equals(copyURL)){
						webview.loadDataWithBaseURL(null, data, null, "utf-8", null);
					}else{
						webview.setWebViewClient(new MyWebViewClient());
						webview.getSettings().setJavaScriptEnabled(true);
						webview.getSettings().setUseWideViewPort(true);
						webview.setWebChromeClient(new DefaultWebChromeClient());
						webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
						webview.getSettings().setLoadWithOverviewMode(true);
						webview.loadUrl(data);
						}
				}
			}
			
			@Override
			public void onFailed(String Message) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	// 关联webview 类
		class MyWebViewClient extends WebViewClient {

			// 加载结束的时候
			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
			}

		}
	
	/**
	 * 设置全屏
	 */
	private void setFullScreen() {
		// 设置全屏的相关属性，获取当前的屏幕状态，然后设置全屏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// 全屏下的状态码：1098974464
		// 窗口下的状态吗：1098973440
	}
	
	/**
	 * 退出全屏
	 */
	private void quitFullScreen() {
		// 声明当前屏幕状态的参数并获取
		final WindowManager.LayoutParams attrs = getWindow().getAttributes();
		attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().setAttributes(attrs);
		getWindow()
				.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
	}
	
	private class DefaultWebChromeClient extends WebChromeClient {
		// 一个回调接口使用的主机应用程序通知当前页面的自定义视图已被撤职

		// 进入全屏的时候
		@Override
		public void onShowCustomView(View view, CustomViewCallback callback) {
			// 将video放到当前视图中
			video.addView(view);
			// 横屏显示
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			// 设置全屏
			setFullScreen();
		}

		// 退出全屏的时候
		@Override
		public void onHideCustomView() {
			// 用户当前的首选方向
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			// 退出全屏
			quitFullScreen();
		}

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			super.onProgressChanged(view, newProgress);
		}
	}


}
